package com.yl.auth.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.easyexcel.listener.ExcelLaoRenDTOListener;
import com.yl.auth.mapper.LaorenMapper;
import com.yl.auth.model.entity.Laoren;
import com.yl.auth.model.vo.LaoFangChuangVo;
import com.yl.auth.mapper.BingshiMapper;
import com.yl.auth.model.vo.LaorenVo;
import com.yl.auth.service.LaorenService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yl
 * @since 2023-06-15
 */
@Service
@Slf4j
public class LaorenServiceImpl extends ServiceImpl<LaorenMapper, Laoren> implements LaorenService {

    @Resource
    private BingshiMapper bingshiMapper;
    @Resource
    private LaorenMapper laorenMapper;
    @Override
    public void savejianKuang(Laoren laoren) {
        if(laoren.getBingshi().getLaorenId()!=null) {
            //声明一个空的字符
            String str = "";
            //循环出现疾病的每个症状
            for (int i = 0; i < laoren.getBingshi().getNewbingshis().length; i++) {
                //判断如果是长度是整体的长度减一的时候不加逗号，如果等于的话就加逗号为了让最后一个没有逗号结束
                if (i == laoren.getBingshi().getNewbingshis().length - 1) {
                    str += laoren.getBingshi().getNewbingshis()[i];
                } else {
                    str += laoren.getBingshi().getNewbingshis()[i] + ",";
                }
            }
            //随后set进新的疾病中
            laoren.getBingshi().setNewbingshi(str);
            //为旧的疾病做处理
            String str2 = "";
            for (int i = 0; i < laoren.getBingshi().getOldbingshis().length; i++) {
                if (i == laoren.getBingshi().getOldbingshis().length - 1) {
                    str2 += laoren.getBingshi().getOldbingshis()[i];
                } else {
                    str2 += laoren.getBingshi().getOldbingshis()[i] + ",";
                }
            }
            laoren.getBingshi().setOldbingshi(str2);
            bingshiMapper.updateById(laoren.getBingshi());
        }else {
            //声明一个空的字符
            String str = "";
            //循环出现疾病的每个症状
            for (int i = 0; i < laoren.getBingshi().getNewbingshis().length; i++) {
                //判断如果是长度是整体的长度减一的时候不加逗号，如果等于的话就加逗号为了让最后一个没有逗号结束
                if (i == laoren.getBingshi().getNewbingshis().length - 1) {
                    str += laoren.getBingshi().getNewbingshis()[i];
                } else {
                    str += laoren.getBingshi().getNewbingshis()[i] + ",";
                }
            }
            //随后set进新的疾病中
            laoren.getBingshi().setNewbingshi(str);
            //为旧的疾病做处理
            String str2 = "";
            for (int i = 0; i < laoren.getBingshi().getOldbingshis().length; i++) {
                if (i == laoren.getBingshi().getOldbingshis().length - 1) {
                    str2 += laoren.getBingshi().getOldbingshis()[i];
                } else {
                    str2 += laoren.getBingshi().getOldbingshis()[i] + ",";
                }
            }
            laoren.getBingshi().setOldbingshi(str2);
            Long id = laorenMapper.maxLaoId();
            laoren.getBingshi().setLaorenId(id);
            bingshiMapper.insert(laoren.getBingshi());
        }


    }

    @Override
    public Page<LaoFangChuangVo> getPage(Page<LaoFangChuangVo> page1, String name,String searchParam) {
        return laorenMapper.getPage(page1,name,searchParam);
    }

    @Override
    public List<Laoren> find() {
        return laorenMapper.find();
    }

    @Override
    public List<Laoren> updateFind() {
        return laorenMapper.updateFind();
    }

    @Override
    public Page<LaoFangChuangVo> findAll(Page<LaoFangChuangVo> page1, String name) {
        return laorenMapper.findAll(page1,name);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void improtData(InputStream inputStream) {
        //这里需要指定读用哪个class去读.然后读取第一个sheet文件流会自动关闭
        EasyExcel.read(inputStream, Laoren.class,new ExcelLaoRenDTOListener(baseMapper)).sheet().doRead();
        log.info("improtData finished");
    }

    @Override
    public String get(Long id) {
        return laorenMapper.get(id);
    }

    @Override
    public List<Laoren> listlaoren(Integer id) {
        return laorenMapper.listlaoren(id);
    }

    @Override
    public LaorenVo getLao(Long id) {
        return laorenMapper.getLao(id);
    }
}
